﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Test;
using Common.Utilities.TextMatch;

namespace PdfLib.Test.Searcher
{
    [ManualTestCase("String searcher", "Searcher")]
    public partial class TestCase_StringSearcher : UserControl
    {
        public enum StringSearchMathod
        {
            LongestSubstring = 0,
            LongestSubsequence = 1,
            Soundex = 2,
            Metaphone = 3,
            FuzzySearch = 4,
            EditDistance = 5,
            ShiftDistance = 6
        }

        public TestCase_StringSearcher()
        {
            InitializeComponent();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            StringSearchMathod method = StringSearchMathod.FuzzySearch;
            if(this.cbo_Method.SelectedIndex>=0)
                method = (StringSearchMathod) this.cbo_Method.SelectedIndex;

            double score = 0;
            switch (method)
            {
                case StringSearchMathod.FuzzySearch:
                    score = StringUtil.FuzzyStringMatch(this.rt_Content.Text, this.txt_Pattern.Text, false);
                    break;
                case StringSearchMathod.LongestSubsequence:
                    score = (double) StringUtil.LongestCommonSubsequence(this.rt_Content.Text, this.txt_Pattern.Text)/
                            this.txt_Pattern.Text.Length;
                    break;
                case StringSearchMathod.LongestSubstring:
                    score = (double) StringUtil.LongestCommonSubString(this.rt_Content.Text, this.txt_Pattern.Text)/
                            this.txt_Pattern.Text.Length;
                    break;
                case StringSearchMathod.Metaphone:
                    MetaphoneConverter metaphoneConverter = new MetaphoneConverter();
                    string str1 = metaphoneConverter.Convert(this.txt_Pattern.Text);
                    string str2 = metaphoneConverter.Convert(this.rt_Content.Text);
                    score = 1.0 - (double) StringUtil.EditDistance(str1, str2, false)/str2.Length;
                    break;
                case StringSearchMathod.Soundex:
                    SoundexConverter soundexConverter = new SoundexConverter();
                    string str3 = soundexConverter.Convert(this.txt_Pattern.Text);
                    string str4 = soundexConverter.Convert(this.rt_Content.Text);
                    score = 1.0 - (double) StringUtil.EditDistance(str3, str4, false)/str3.Length;
                    break;
                case StringSearchMathod.EditDistance:
                    score =(double) StringUtil.EditDistance(this.rt_Content.Text, this.txt_Pattern.Text, false);
                    break;
                case StringSearchMathod.ShiftDistance:
                    score = (double) StringUtil.SiftDistance(this.rt_Content.Text, this.txt_Pattern.Text, 10);
                    break;
                default:
                    score = 0.0;
                    break;
            }
            this.txt_Score.Text = score.ToString();
        }
    }
}
